const Router = require("koa-router");
const register = new Router();
//! 拿到前端传过来的数据  koa-bodyparser
const bodyparser = require("koa-bodyparser");
const db = require("../utils/db");

//! 调用下面这个中间件，就可以拿到前端post过来的数据
register.use(bodyparser());

register.post("/", async (ctx) => {
  console.log(ctx.request.body);
  const { company, software, username, password } = ctx.request.body;
  //? todo 存新账号到数据库
  let insertSql = `insert into user (username, password) VALUES ("${username}", "${password}")`;
  await new Promise((resolve, reject) => {
    return db.query(insertSql, (err, data) => {
      if (err) throw err;
      resolve(data);
    });
  });
  ctx.body = "已注册";
});

module.exports = register;
